﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Globalization;
using System.Windows.Forms;

namespace FAST
{
    [Serializable()]
    public class FileVersioningList
    {

        /// <summary>
        /// Full file path is use as user will tend to move original files around and file might
        /// not stay at the same location, he would want to have a link that connects the 
        /// original file and its versions together, even if the original is being moved around.
        /// </summary>
        private String fullFP; 
        private int oldestVersion;  
        private int latestVersion; 
        private int limit;
        public List<FileHandler> versions;
        private String versionedFilesLocation;


        //Method name : FileVersioningList() : N.A for constructors.
        //Parameters  : None.
        //Description : An instance of the FileVersioningList constructor which contains the original file's full file path 'fullFP', an int oldestVersion, an int latestVersion, an int limit, a List<FileHandler> versions and a string 'versionedFilesLocation'.
        //Exceptions  : None.
        public FileVersioningList()
        {
            fullFP = "";
            oldestVersion = 0;
            latestVersion = -1;
            limit = 5; //default set at 5
            versions = new List<FileHandler>();
            versionedFilesLocation = Application.StartupPath + @"\Sessions\Version\"; //location used to store all the versioned files
        }

        //Method name : getListOfFH() : List<FileHandler>
        //Parameters  : None.
        //Description : Returns a list of FileHandler objects.
        //Exceptions  : None.
        public List<FileHandler> getListOfFH()
        {
            return this.versions;
        }

        //Method name : getVersionedFilesLocation() : String
        //Parameters  : None.
        //Description : Returns the path of versionedFilesLocation as a string.
        //Exceptions  : None.
        public String getVersionedFilesLocation()
        {
            return this.versionedFilesLocation;
        }

        //Method name : getFullFP() : String
        //Parameters  : None.
        //Description : Returns the path of the original file's full file path as a string.
        //Exceptions  : None.
        public String getFullFP()
        {
            return this.fullFP;
        }

        //Method name : setFullFP() : void
        //Parameters  : str(type String)
        //Description : Sets the path of the original file's full file path.
        //Exceptions  : None.
        public void setFullFP(String str)
        {
            this.fullFP = str;
        }

        //Method name : getOldestVersion() : int
        //Parameters  : None.
        //Description : Returns the oldest version as an integer.
        //Exceptions  : None.
        public int getOldestVersion()
        {
            return this.oldestVersion;
        }

        //Method name : setOldestVersion() : void
        //Parameters  : ver(type int)
        //Description : Sets the oldest version of the file to 'ver'.
        //Exceptions  : None.
        public void setOldestVersion(int ver)
        {
            this.oldestVersion = ver;
        }
        
        //Method name : setVersionLimit() : void
        //Parameters  : lmt(type int)
        //Description : Sets the version limit of a file's versions.
        //Exceptions  : None.
        public void setVersionLimit(int lmt)
        {   
            this.limit = lmt;
        }


        //Method name : getVersionLimit() : int
        //Parameters  : None.
        //Description : Returns the version limit of a file.
        //Exceptions  : None.
        public int getVersionLimit()
        {
            return this.limit;
        }


        //Method name : getLatestVersionValue() : int
        //Parameters  : None.
        //Description : Returns the integer value of the latest version of the file.
        //Exceptions  : None.
        public int getLatestVersionValue()
        {
            return this.latestVersion;
        }

        //Method name : setLatestVersionValue() : void
        //Parameters  : ver(type int)
        //Description : Sets the integer value of the latest version of the file.
        //Exceptions  : None.
        public void setLatestVersionValue(int ver)
        {//sets the value of the latest version
            this.latestVersion = ver;
        }

        //Method name : setNextLatestVersion() : int
        //Parameters  : ver(type int)
        //Description : Sets the integer value of the latest version of the file by incrementing one to the latest version value.
        //Exceptions  : None.
        public int setNextLatestVersion(int ver)
        {//increments the next version value by 1
            return ver + 1;
        }

        //Method name : getListCount() : int
        //Parameters  : None.
        //Description : Returns the number of items in this list.
        //Exceptions  : None.
        public int getListCount()
        {//returns the number of FileHandler objects in list
            return this.versions.Count;
        }


        //Method name : createVersionInList() : void
        //Parameters  : file(type FileHandler)
        //Description : Creates a version of the current file in this list.
        //Exceptions  : FileNotFound Exception: Invalid file path or file does not exist.
        public void createVersionInList(FileHandler file)
        {//creates a NEXT version into this list
            FileHandler newVersion;
            //update this copy to the next latest version with the updated filepath and ends with e.g. abc_v[0].ext
            String newPathName = setNewVersionPathName(file.fullFilePath);
            String temp = newPathName.Substring(0, newPathName.LastIndexOf(@"\") + 1);
            //checks if backup folder exists, else create for user
            if (!Directory.Exists(temp))
            {
                Directory.CreateDirectory(temp);
            }
            //creates a copy of the current file handler with the new version filepath
            FileInfo MyFile = new FileInfo(file.fullFilePath);
            MyFile.CopyTo(newPathName, true);
            newVersion = new FileHandler(newPathName, file.subFilePath);
            newVersion.setVersioningIdx(file.getVersioningIdx());
            (this.getListOfFH()).Add(newVersion);//add this new file into the current list
        }

        //Method name : setNewVersionPathName() : String
        //Parameters  : oldfp(type String)
        //Description : Generates a new version path name with the old path name.
        //Exceptions  : FileNotFound Exception: Invalid file path or file does not exist.
        public String setNewVersionPathName(String oldfp)
        {
            String filename = oldfp.Substring(oldfp.LastIndexOf("\\") + 1); //format eg. abc
            filename = filename.Substring(0, filename.LastIndexOf("."));
            String extension = oldfp.Substring(oldfp.LastIndexOf(".") + 1);//format eg. txt
            int newVersionNum = setNextLatestVersion(this.getLatestVersionValue());
            setLatestVersionValue(newVersionNum);
            String drive = oldfp.Substring(0, oldfp.LastIndexOf(@":\"));
            drive = drive + "_drive"; //changes 'C:\' to 'c_drive' folder
            String pathAfterRoot = oldfp.Substring(oldfp.IndexOf(@":\") + 1);
            pathAfterRoot = pathAfterRoot.Substring(0, pathAfterRoot.LastIndexOf(@"\"));
            String newParentRootFullFP = this.versionedFilesLocation + drive + @"\" + pathAfterRoot + @"\";
            if (!Directory.Exists(newParentRootFullFP))
            {
                Directory.CreateDirectory(newParentRootFullFP);
            }
            String newVersionFP = newParentRootFullFP + filename + "_v[" + newVersionNum + "]." + extension;
            return newVersionFP;
        }

        //Method name : setVersionList() : void
        //Parameters  : versions(type List<FileHandler>)
        //Description : Sets the current versions list to this 'versions' list
        //Exceptions  : FileNotFound Exception: Invalid file path or file does not exist.
        public void setVersionList(List<FileHandler> versions)
        {
            this.versions = versions;
        }

    }
}
